$(document).ready(function () {
	$("#baocuns").click(function(){
		 if(!checkform("#form_bd2s")){
			  return;
		  }
			var params =$("#form_bd2s").serializeObject();	
			if(params.parentid==""){
				params.parentid=0;
			}
			var json = ajaxResult("get",false,"../../mbxtgljgservlet.insert.do",params);
		  if(json.status=="OK"){
			  $('#exampleModal2').modal('hide');
			  alertFunc("添加成功");
			 // $('ul li').remove();
			  if(params["dsf"]==1){
			       initTree("treeDemo",1);
			  }else{
				  initTree("treeDemoG",2);
			  }
			  $("#form_bd2s")[0].reset();
			//  refreshTable("#userInfo");
			
		  }else{
			  $('#exampleModal2').modal('hide');
			  alertFunc("添加失败");
			  refreshTable("#userInfo");
			
		  }
	});
	
	$("#baocun").click(function(){
		 if(!checkform("#form_bd2")){
			  return;
		  }
		 
			window.top.bootbox.setLocale("zh_CN");
			window.top.bootbox.confirm("确认修改该机构信息吗？",function(r){
				if(r){
					var params =$("#form_bd2").serializeObject();	
					params.zdbh=zdbh;
				 var json = ajaxResult("get",false,"../../mbxtgljgservlet.update.do",params);
				  if(json.status=="OK"){
					  alertFunc("修改成功");
					  $('ul li').remove();
					  initTree("treeDemo",1);
					  initTree("treeDemoG",2);
					  
					  $("#form_bd2s")[0].reset();
					  refreshTable("#userInfo");
					  $("#form_bd2")[0].reset();
					  $('#exampleModal').modal('hide');
				  }else{
					  alertFunc("修改失败");
					  $('#exampleModal').modal('hide');
					
				  }
				}
			});

	});
	var setting = {
			
			
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onClick: zTreeOnCheck
			}
		};
	
	var jsonTree = ajaxResult("get",false,"../../mbxtgljgservlet.getDataByTree.do",{"dsf":"1"});	
	var jsonTreeG = ajaxResult("get",false,"../../mbxtgljgservlet.getDataByTree.do",{"dsf":"2"});	
	$.fn.zTree.init($("#treeDemo"), setting, jsonTree.data);
	$.fn.zTree.init($("#treeDemoG"), setting, jsonTreeG.data);
	//createTable('#userInfo','../../mbxtgljgservlet.getDataByWhere.do','zdbh',tblColTitle,null,null);
	
});
var tblColTitle = [ 
	{ title: '序号', width: 50, align: "center", formatter: function (value, row, index) { return index + 1; } },
	{ title: '机构名称', field: 'jgmc' ,width: 95},
	{ title: '机构状态', field: 'jgzx' ,width: 95, formatter: function (value, row, index) { return "已注销"; }},
	{ title: '机构简介', field: 'jgjj' ,width: 95},
	{ title: '操作', field: 'zdbh' ,width: 95,formatter: function (value, row, index) 
		{ 
			//console.log(value);console.log(row);console.log(index);
			return "<a onclick='updatenozx("+value+")'>取消注销</a>"; }},
	
	
];

var zdbh="";
function zTreeOnCheck(event, treeId, treeNode) {
	
	zdbh=treeNode.id;
};

function zhuxiao(dsf){
	var doc="";
	if(dsf==1){
		doc="treeDemo";
	}else{
		doc="treeDemoG";
	}
	
	if(zdbh==""){
		alertFunc("请点击想要注销的机构");
		return;
	}
	
	window.top.bootbox.setLocale("zh_CN");
	window.top.bootbox.confirm("确认注销该机构信息吗？",function(r){
		if(r){
			   var json = ajaxResult("get",false,"../../mbxtgljgservlet.delete.do",{zdbh:zdbh});
				  if(json.status=="OK"){
					  alertFunc("注销成功");
					  //$('ul li').remove();
					  initTree(doc,dsf);
					  //refreshTable("#userInfo");
				  }else{
					  alertFunc("注销失败,该机构下可能还存在用户");
					  //refreshTable("#userInfo");
					
				  }
		}
	});
	
}
var qjbl;
function updatenozx(zdbh){
	var result = ajaxResult("get",false,"../../mbxtgljgservlet.updatenozx.do",{"zdbh":zdbh});	
	if(result.status=='OK'){
		alertFunc("取消注销成功！");
		showZxList(qjbl);
		if(qjbl==1){
			initTree("treeDemo",1);
		}else{
			initTree("treeDemoG",2);
		}
		$("#form_bd2s")[0].reset();
	}
}
function xiugai(){
	
	if(zdbh==""){
		alertFunc("请点击想要修改的机构");
		 $(".xg").removeAttr("data-toggle");
		 $(".xg").removeAttr("data-target");
		return;
	}
	 $(".xg").attr("data-toggle","modal");
	 $(".xg").attr("data-target","#exampleModal");
	
	 var formJson = ajaxResult("get",false,"../../mbxtgljgservlet.getDataById.do",{zdbh:zdbh});
	 loadData("#form_bd2",formJson.data);
}

function xz(dsf){
	var mzjson = ajaxResult("get",false,"../../mbxtgljgservlet.getDataOneJg.do",{"dsf":dsf});
	loadSelect("#parentid",mzjson.data,'zdbh','jgmc');
	$("#dsf").val(dsf);
}

function initTree(doc,dsf){
var setting = {
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onClick: zTreeOnCheck
			}
		};
	var jsonTree = ajaxResult("get",false,"../../mbxtgljgservlet.getDataByTree.do",{"dsf":dsf});	
	$.fn.zTree.init($("#"+doc), setting, jsonTree.data);
}
function showZxList(dsf){
	qjbl = dsf;
	var formParams={};
	formParams["dsf"]=dsf;
	$("#zxList").bootstrapTable('destroy');
	createTable('#zxList','../../mbxtgljgservlet.getDataByWhere.do','zdbh',tblColTitle,formParams,null);
}




